﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.AlbLab.GEP.Domain.Entities;

namespace Com.AlbLab.CR.Web.controls
{
    public partial class Response_NST_EL0_AP0_VP0_CF0_CT0 : System.Web.UI.UserControl, IResponse
    {

        public event SaveEventHandler Save;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void Initialize(GEP_TESTATA t)
        {
        hidPrtId.Value = t.Id_tes_cod_pratica.ToString();
            hidTipo.Value = t.Tes_Cod_Servizio;
            if (t.Gep_risposta.Response == null)
            {
                cod_servizio.Text = t.Tes_Cod_Servizio;
                piva_utente.Text = t.Tes_Piva_Utente;
                piva_distributore.Text = t.Tes_Piva_Distributore;
                cod_prat_utente.Text = t.Id_tes_cod_pratica.ToString();
                cod_prat_distr.Text = t.Tes_Cod_Prat_Utente.Trim();

                misuratore_elettronico.SelectedValue = t.Tes_Mis_Eleco.ToUpper();
                matr_mis_attiva.Text = t.Tes_Matr_Att;
                matr_mis_reattiva.Text = t.Tes_Matr_Rea;
                matr_mis_potenza.Text = t.Tes_Matr_Pot;

                if (!hidPrtId.Value.Equals("EL1"))
                    cod_pod.Text = t.Tes_Pod;

                if (!hidPrtId.Value.Equals("EL1") && !hidPrtId.Value.Equals("VA0"))
                {
                    nome_cliente.Text = t.Tes_Ragsoc_Cli;
                    cod_fis_cliente.Text = t.Tes_Codfis_Cli;
                    piva_cliente.Text = t.Tes_Piva_Utente;
                    indirizzo_fornitura.Text = t.Tes_Ind_Fornit;
                }


                GEP_RISPOSTA res = new GEP_RISPOSTA();
                res.Resp_Id = Convert.ToDecimal(hidPrtId.Value);
                res.Tipo_servizio = hidTipo.Value;
                res.Response = buildRecord();

                if (Save != null)
                    Save(this, res, true);
            }
        }

        public System.Xml.XmlDocument buildRecord()
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml("<Response></Response>");

            foreach (Control c in this.Controls)
            {
                if (c.GetType() == typeof(TextBox))
                {
                    TextBox t = (TextBox)c;
                    System.Xml.XmlElement el = doc.CreateElement(t.ID);
                    el.InnerXml = t.Text;
                    doc.DocumentElement.AppendChild(el);
                }
                else if (c.GetType() == typeof(DropDownList))
                {
                    DropDownList t = (DropDownList)c;
                    System.Xml.XmlElement el = doc.CreateElement(t.ID);
                    el.InnerXml = t.SelectedValue;
                    doc.DocumentElement.AppendChild(el);
                }
            }
            return doc;
        }

        public void setdRecord(System.Xml.XmlDocument doc)
        {
            foreach (System.Xml.XmlElement el in doc.DocumentElement.ChildNodes)
            {
                if (!string.IsNullOrEmpty(el.InnerXml))
                {
                    Control cc = this.FindControl(el.Name);
                    if (cc.GetType() == typeof(TextBox))
                        ((TextBox)cc).Text = el.InnerXml;
                    else if (cc.GetType() == typeof(DropDownList))
                        ((DropDownList)cc).SelectedValue = el.InnerXml;
                }
            }


        }

        public void btnSave_OnClick(object sender, EventArgs e)
        {
            GEP_RISPOSTA res = new GEP_RISPOSTA();
            res.Resp_Id = Convert.ToDecimal(hidPrtId.Value);
            res.Tipo_servizio = hidTipo.Value;
            res.Response = buildRecord();

            if (Save != null)
                Save(this, res, false);
        }
    }
}